package servicos;

import java.util.ArrayList;
import java.util.Iterator;
import classes.Evento;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

public class Conexao {

	private static String URI = "http://192.168.1.143/e-vent-sw/servico/"; 
	
	public ArrayList<Evento> listarEventos(){
		String[] json = new WebService().get(URI + "/evento.php/listaeventos");
		ArrayList<Evento> eventos = new ArrayList<Evento>();		

		if (json[0].equals("200") && json[1].length() > 4) {													

			JsonParser parser = new JsonParser();

			JsonArray jsonArray = parser.parse(json[1]).getAsJsonArray();

			@SuppressWarnings("rawtypes")
			Iterator iterator = jsonArray.iterator();

			while (iterator.hasNext()){
				JsonElement json2 = (JsonElement)iterator.next();
				Gson gson = new Gson();											    
				Evento evento = gson.fromJson(json2, Evento.class);
				eventos.add(evento);
			}

			return eventos;

		} else {
			return null;			
		}
		
	}
	
}
